from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse, FileResponse
from reportlab.pdfgen import canvas
import io
import re
import json
from .models import *
from user.models import *
from order.models import *
from room.models import *

# Create your views here.
@csrf_exempt
def create_workorder(request):
    if request.method == 'POST':
        data_json = json.loads(request.body)
        username = request.session.get('username', "")
        if username == "":
            return JsonResponse({'errno': 1002, 'msg': "请先登录"})
        else:
            uid = User.objects.get(username=username).id
            rid = data_json.get('rid', 0)
            if Room.objects.filter(id=rid).exists():
                WorkOrder.objects.create(uid=uid, rid=rid, info = data_json.get('info', ""), 
                                    type = data_json.get('type', 0))
                return JsonResponse({'errno': 0, 'msg': "创建成功"})
            else:
                return JsonResponse({'errno': 1003, 'msg': "用户未租房或订单未确认"})
    else:
        return JsonResponse({'errno': 1001, 'msg': "请求方式错误"})


@csrf_exempt
def assign_worker(request):
    if request.method == 'POST':
        data_json = json.loads(request.body)
        username = request.session.get('username', "")
        if username == "":
            return JsonResponse({'errno': 1002, 'msg': "请先登录"})
        user = User.objects.get(username=username)
        if user.admin:
            worker = data_json.get('worker', 0)
            wid = data_json.get('wid', 0)
            if not WorkOrder.objects.filter(id=wid).exists():
                return JsonResponse({'errno': 1004, 'msg': "工单不存在"})
            elif not User.objects.filter(id=worker).exists():
                return JsonResponse({'errno': 1004, 'msg': "工人不存在"})
            elif not User.objects.get(id=worker).worker:
                return JsonResponse({'errno': 1005, 'msg': "该用户不是工人"})
            else:
                workorder = WorkOrder.objects.get(id=wid)
                workorder.worker = worker
                workorder.status = 2
                workorder.save()
                return JsonResponse({'errno': 0, 'msg': "分配成功"})
        else:
            return JsonResponse({'errno': 1003, 'msg': "非管理员用户"})
    else:
        return JsonResponse({'errno': 1001, 'msg': "请求方式错误"})


@csrf_exempt
def ensure_workorder(request):
    if request.method == 'POST':
        data_json = json.loads(request.body)
        wid = data_json.get('wid', 0)
        username = request.session.get('username', "")
        if username == "":
            return JsonResponse({'errno': 1002, 'msg': "请先登录"})
        if WorkOrder.objects.filter(id=wid).exists():
            workorder = WorkOrder.objects.get(id=wid)
            if(workorder.uid == User.objects.get(username=username).id):
                workorder.status = 3
                workorder.save()
                return JsonResponse({'errno': 0, 'msg': "确认工单成功"})
            else:
                return JsonResponse({'errno': 1004, 'msg': "用户登录信息错误"})
        else:
            return JsonResponse({'errno': 1003, 'msg': "工单不存在"})
    else:
        return JsonResponse({'errno': 1001, 'msg': "请求方式错误"})


@csrf_exempt
def search_workorders(request):
    if request.method == 'POST':
        data_json = json.loads(request.body)
        username = request.session.get('username', "")
        if username == "":
            return JsonResponse({'errno': 1002, 'msg': "请先登录"})
        elif User.objects.get(username=username).admin:
            res = []
            for workorder in WorkOrder.objects.all():
                res.append(workorder.get_info())
            return JsonResponse({'errno': 0, 'msg': "查询成功", 'content': res})
        else:
            res = []
            for workorder in WorkOrder.objects.filter(uid=User.objects.get(username=username).id):
                res.append(workorder.get_info())
            return JsonResponse({'errno': 0, 'msg': "查询成功", 'content': res})
    else:
        return JsonResponse({'errno': 1001, 'msg': "请求方式错误"})


@csrf_exempt
def lookup_workorder(request):
    if request.method == 'POST':
        data_json = json.loads(request.body)
        username = request.session.get('username', "")
        if username == "":
                return JsonResponse({'errno': 1002, 'msg': "请先登录"})
        else:
            wid = data_json.get('wid', 0)
            user = User.objects.get(username=username)
            if WorkOrder.objects.filter(id=wid).exists():
                workorder = WorkOrder.objects.get(id=wid)
                if(workorder.uid == user.id or user.admin):
                    return JsonResponse({'errno': 0, 'msg': "查询成功", 'content': workorder.get_info()})
                else:
                    return JsonResponse({'errno': 1004, 'msg': "用户登录信息错误"})
            else:
                return JsonResponse({'errno': 1003, 'msg': "工单不存在"})
    else:
        return JsonResponse({'errno': 1001, 'msg': "请求方式错误"})
    
    
@csrf_exempt
def delete_workorder(request):
    if request.method == 'POST':
        data_json = json.loads(request.body)
        wid = data_json.get('wid', 0)
        username = request.session.get('username', "")
        if username == "":
            return JsonResponse({'errno': 1002, 'msg': "请先登录"})
        uid = User.objects.get(username=username).id
        if WorkOrder.objects.filter(id=wid).exists():
            workorder = WorkOrder.objects.get(id=wid)
            if(workorder.uid == uid):
                workorder.delete()
                return JsonResponse({'errno': 0, 'msg': "删除工单成功"})
            else:
                return JsonResponse({'errno': 1004, 'msg': "用户登录信息错误"})
        else:
            return JsonResponse({'errno': 1003, 'msg': "订单不存在"})
    else:
        return JsonResponse({'errno': 1001, 'msg': "请求方式错误"})
